package com.jasonchoi.redisqueue.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * Redisson 配置类
 * @Author: JasonChoi
 * @Date: 2019/12/24 18:04
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    String redisHost;

    @Value("${spring.redis.port}")
    Integer redisPort;

    @Value("${spring.redis.database}")
    Integer redisDatabase;

    @Bean
    public RedissonClient redissonClient() throws IOException {
        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("properties/redisson-config.yml"));
        config.useSingleServer().setDatabase(redisDatabase)
              .setAddress(String.format("redis://%s:%s", redisHost, redisPort));
        return Redisson.create(config);
    }
}
